package com.demo.controller;

import com.demo.util.ControllerManager;
import com.demo.util.Tabs;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;

import static com.demo.util.ControllerManager.leftController;
import static com.demo.util.ControllerManager.mainController;

public class Step3Controller implements Initializable {
    @FXML
    private AnchorPane ap;
    @FXML
    private TextArea resultTextArea;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        // 邮件预览
        System.out.println("Step3Controller.Init");
        ControllerManager.step3Controller = this;

        // 读取日志并显示
        try {
            String logContent = readLogFile();
            resultTextArea.setText(logContent);
        } catch (IOException e) {
            resultTextArea.setText("日志读取失败: " + e.getMessage());
        }
    }

    private String readLogFile() throws IOException {
        // 从配置文件读取日志路径
        String logDir = System.getProperty("LOG_DIR", "../logs");
        Path logPath = Paths.get(logDir, "app.log");

        return Files.readString(logPath);
    }

    @FXML
    protected void onBackStepClick() { //返回
        Stage stage = (Stage) ap.getScene().getWindow();
        stage.setTitle("Step1!");
        Scene scene1 = Tabs.getScene(Tabs.STEP1_KEY);
        stage.setScene(scene1);
        stage.show();
        leftController.setCurrentStep(1);
    }

    @FXML
    protected void onCloseClick() {
        mainController.onCloseClick();
        System.out.println("close");
    }

}
